﻿#include "kv_store.h"

#if ARRAYENGINEENABLE

kvs_array_t global_array;

int kvs_array_create(kvs_array_t *inst)
{
    inst->table = (kvs_array_item_t *)calloc(KVS_ARRAY_SIZE,sizeof(kvs_array_item_t));
    if (inst->table == NULL)
    {
        return -1;
    }
    inst->total = 0;
    inst->idx = 0;
    return 0;
}

int kvs_array_free(kvs_array_t *inst)
{
    if (inst->table)
    {
        free(inst->table);
        return 0;
    }
    return -1;
}

int kvs_array_set(kvs_array_t *inst, char *key, char *value)
{
    if (key == NULL || value == NULL)
    {
        return -1;
    }

    // 验证是否存在
    char *pIsExitStrValue = NULL;
    for (int i = 0; i < inst->idx; i++)
    {
        pIsExitStrValue = kvs_array_get(inst, key);
        if (pIsExitStrValue != NULL)//存在
        {
            return 1;
        }
    }

    // 开辟新空间 存放新元素
    if (pIsExitStrValue == NULL)
    {
        for (int i = 0; i < inst->idx; i++)
        {
            if (inst->table[i].key == NULL)//利用以前位置
            {
                inst->table[i].key = (char *)calloc(strlen(key) + 1,1);
                inst->table[i].value = (char *)calloc(strlen(value) + 1,1);
                strncpy(inst->table[i].key, key, strlen(key));
                strncpy(inst->table[i].value, value, strlen(value));
                inst->total += 1;
                return 0;
            }
        }

        //重新开辟
        inst->table[inst->total].key = (char *)calloc(strlen(key) + 1,1);
        inst->table[inst->total].value = (char *)calloc(strlen(value) + 1,1);
        strncpy(inst->table[inst->total].key, key, strlen(key));
        strncpy(inst->table[inst->total].value, value, strlen(value));
        inst->total += 1;
        inst->idx += 1;
        return 0;
    }
	return -1;
}

char *kvs_array_get(kvs_array_t *inst, char *key)
{
    if (inst == NULL || key == NULL)
    {
        return NULL;
    }

    for (int i = 0; i < inst->idx; i++)
    {
#ifdef PRINTFCMODE
        printf("element:%s\n",inst->table[i].key);
#endif
        if (inst->table[i].key == NULL)
        {
            continue;
        }
        else if (strcmp(key, inst->table[i].key) == 0)
        {
            return inst->table[i].value;
        }
    }

#ifdef PRINTFCMODE
    printf("key:%s\n", key);
#endif

    return NULL;
}

int kvs_array_mod(kvs_array_t *inst, char *key, char *value)
{
    if (inst == NULL || key == NULL)
        return -1;
    char *pValue = NULL;
    for (int i = 0; i < inst->idx; i++)
    {
        pValue = kvs_array_get(inst, key);
        if (pValue != NULL)
        {
            memcpy(pValue,value,strlen(value)+1);
            return 0;
        }
    }
    if (pValue == NULL)
        return -1;
	return -1;
}

int kvs_array_del(kvs_array_t *inst, char *key)
{
    if (inst == NULL || key == NULL)
        return -1;

    for (int i = 0; i < inst->idx; i++)
    {
        if (strcmp(inst->table[i].key,key) == 0)
        {
            free(inst->table[i].key);
            inst->table[i].key = NULL;
            free(inst->table[i].value);
            inst->table[i].value = NULL;
            inst->total -= 1;
            return 0;
        }
    }
	return -1;
}

int kvs_array_exist(kvs_array_t *inst, char *key)
{
    int result = 0;
    if (inst == NULL || key == NULL)
        return -1;
    char *pValue = NULL;
    for (int i = 0; i < inst->idx; i++)
    {
        pValue = kvs_array_get(inst, key);
        if (pValue != NULL)
            result = 1;
    }
    return result;
}

#endif